package com.culturalCenter.dataCenter.schedule.passenger;

import com.alibaba.fastjson.JSONObject;
import com.culturalCenter.dataCenter.Utils.RedisUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.passengerflow.PassengerFlowLoginInfoResponse;
import com.culturalCenter.dataCenter.service.passengerflow.PassengerFlowLoginService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 定时任务_客流系统定时登录来获取新Token（Token30分钟失效）
 *
 * @Author zhao
 * @Date 2021-6-25 15:46
 */
@Component
@Slf4j
@Async("dataThreadExecutor")
public class TokenKeepAlive {

    public static String tokenPassengerFlow = "eb1fd938a48ab74e2ea0509801277429";
    public static String userIdPassengerFlow = "1";
    public static String userNamePassengerFlow = "system";
    @Value("${passengerFlow.userName}")
    private String userName;
    @Value("${passengerFlow.password}")
    private String password;
    @Resource
    private PassengerFlowLoginService passengerFlowLoginService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RedissonClient redisson;
    @Value("${zhaji.hostname}")
    private String zhaJiHostname;
    @Value("${zhaji.userName}")
    private String zjUserName;
    @Value("${zhaji.password}")
    private String zjPassword;
    @Value("${zhaji.xkey}")
    private String xkey;
    @Resource
    private RestTemplate restTemplate;

    /**
     * for test
     */
    // @PostConstruct
    public void initLock() {
        for (int i = 1; i <= 1; i++) {
            redisUtil.del("passenger_lock" + i);
        }
    }

/*    @Scheduled(cron = "${cron.passenger}")
    // @Scheduled(cron = "${cron.test}")
    public void getToken() {
        String lockKey = "passenger_lock1";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            redissonLock.lock();
            PassengerFlowLoginInfoResponse loginInfo = passengerFlowLoginService.getLoginReturnInfo(userName, password);
            if (Objects.nonNull(loginInfo)) {
                tokenPassengerFlow = loginInfo.getToken();
                userIdPassengerFlow = loginInfo.getId();
                userNamePassengerFlow = loginInfo.getLoginName();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            redissonLock.unlock();
        }
    }*/
//    @Scheduled(cron = "${cron.zhaji}")
    public void getZhaJiToken() {
        log.warn("从图创获取token...");
        HttpHeaders headers = new HttpHeaders();
        headers.add("x-key", xkey);
        HttpEntity entity = new HttpEntity(headers);
        JSONObject result = restTemplate.postForObject(zhaJiHostname + "?" + "userName=" + zjUserName + "&password=" + zjPassword, entity, JSONObject.class);
        log.warn("result：{}",result);
        String token = result.getJSONObject("data").getString("token");

        redisUtil.set("zhaji-token",token);
//        log.warn("图创闸机token：{}",token);
        return ;
    }

}
